<script setup lang='ts'>
import { ref } from 'vue';
import { type LoginParams} from '@/types/user'
import { ElMessage, type FormRules } from 'element-plus';
import { ElForm } from 'element-plus';
import { useRouter } from 'vue-router'
import { loginAPI } from '@/apis/user';
import { useStore } from '@/'

const formData = ref<LoginParams>({
  mobile: '13777777777',
  code: '246810'
})
const checked =ref<boolean>(false)

const formrules = ref<FormRules<LoginParams>>({
  mobile: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 6, message: '验证码长度为6位', trigger: 'blur' }
  ]
})

const formRef = ref<InstanceType<typeof ElForm>>()

const router = useRouter()

const store=useStore()
const handleLogin=async ()=>{
  formRef.value?.validate(async(valid:boolean)=>{
    if(valid){
        const res=await loginAPI (formData.value)
        console.log(res);

        store.setToken(res.data.token)
        // localStorage.setItem('geek-client-pc-token',res.data.data.token)
        router.push('/')
        ElMessage.success('登录成功');
    }
  })
}
</script>

<template>
 <div class="container">
  <el-card>
    <img class="logo" src="@/assets/images/logo.png" alt="" />
    <!-- 表单 -->
     <el-form  ref="formRef" :rules="formrules" :model="formData">
      <el-form-item prop="mobile">
      <el-input v-model="formData.mobile"  placeholder="请输入手机号"></el-input>
     </el-form-item>
     <el-form-item prop="code">
        <el-input v-model="formData.code" placeholder="请输入验证码" show-password></el-input>
     </el-form-item>
     <el-form-item>
      <el-checkbox v-model="checked" >我已阅读并同意「用户协议」和「隐私条款」</el-checkbox>
     </el-form-item>
     <el-form-item>
      <el-button type="primary" :disabled="!checked" @click="handleLogin">登 录</el-button>
     </el-form-item>

     </el-form>
  </el-card>
</div>
</template>

<style scoped>
/*<style scoped让css只作用于当前组件>
   样式 */
   .container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url(@/assets/images/login.png);
  .el-card {
    width: 440px;
    height: 380px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    box-shadow: 0 0 50px rgba(0,0,0,0.1);
    .logo {
      width: 200px;
      display: block;
      margin: 0 auto 20px;
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>
